package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.UnregisterBatteryReportLog;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.hdw.UnregisterBatteryReportLogMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
/**
 * 未注册电池上报日志业务
 *
 * @Date:Created in 2018/7/10 17:22
 * @Author:
 * @Modified By: zjn 2018/7/10 17:22
 */
@Service
public class UnregisterBatteryReportLogService {
    @Autowired
    UnregisterBatteryReportLogMapper unregisterBatteryReportLogMapper;
    /**
    * 分页查询
    *
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/10  17:22
    */
    public Page findPage(UnregisterBatteryReportLog search) {
        Page page = search.buildPage();
        String suffix = DateFormatUtils.format(search.getQueryLogTime(), "yyyyww");
        if (StringUtils.isNotEmpty(unregisterBatteryReportLogMapper.findTableExist(UnregisterBatteryReportLog.UNREGISTER_BATTERY_REPORT_LOG_TABLE_NAME + suffix))) {
            search.setSuffix(suffix);
            page.setTotalItems(unregisterBatteryReportLogMapper.findPageCount(search));
            search.setBeginIndex(page.getOffset());
            page.setResult(unregisterBatteryReportLogMapper.findPageResult(search));
        }
        return page;
    }
    /**
    * 查询未注册电池
    *
    * @Param: [batteryId, createTime]
    * @Return: cc.yugu.yhdw.common.domain.hdw.UnregisterBatteryReportLog
    * @Author By: zjn 2018/7/10  17:22
    */
    public UnregisterBatteryReportLog find(String batteryId, Date createTime) {
        // 按格式输出年（2018）月（07）
        String suffix = DateFormatUtils.format(createTime, "yyyyww");
        UnregisterBatteryReportLog unregisterBatteryReportLog = null;
        // 判断拼接后的表名不为null
        if (StringUtils.isNotEmpty(unregisterBatteryReportLogMapper.findTableExist(UnregisterBatteryReportLog.UNREGISTER_BATTERY_REPORT_LOG_TABLE_NAME + suffix))) {
            unregisterBatteryReportLog = new UnregisterBatteryReportLog();
            unregisterBatteryReportLog.setBatteryId(batteryId);
            unregisterBatteryReportLog.setSuffix(suffix);
            unregisterBatteryReportLog.setCreateTime(createTime);
        }

        return unregisterBatteryReportLogMapper.find(unregisterBatteryReportLog);
    }
}
